var xlsx = require('node-xlsx');
var fs = require('fs');
var os = require('os');

class XlsxHandler{

    constructor(){

    }

    GenerateRange(rd,range,row,col){
        if (col == -1){
            for (let i=0;i<rd.length;i++){
                if (rd[i][1] != 0 && rd[i][0] != rd[i][1]){
                    var tr = {s: {c:i,r:rd[i][0]},e: {c:i,r:rd[i][1]}};
                    range.push(tr);
                }
                rd[i][0] = row;
                rd[i][1] = row;
            }
        }
        else{
            if (rd[col][1] != 0 && rd[col][0] != rd[col][1]){
                var tr = {s: {c:col,r:rd[col][0]},e: {c:col,r:rd[col][1]}};
                range.push(tr);
            }
            rd[col][0] = row;
            rd[col][1] = row;
        }
    }
    
    MergeData(data){
        var rd = [];
        var range = [];
        var merge = true;
    
        if (data.length == 0){
            return;
        }
    
        var len = data[0].length;
        for (let j = 0; j<len; j++){
            var trd = [0,0];
            rd.push(trd);
        }
        var lastcell = data[0];
    
        for (let j = 0; j<data.length; j++){
            len = data[j].length;
            merge = true;
            for(let i=0;i<len;i++){
                if (merge){
                    if (data[j][i] == lastcell[i]){
                        rd[i][1] = j;
                    }
                    else{
                        if (rd[i][0] != rd[i][1]){
                            this.GenerateRange(rd,range,j,i);
                        }
                        rd[i][0] = j;
                        rd[i][1] = j;
                        merge = false;
                    }
                }
                else{
                    if (rd[i][0] != rd[i][1]){
                        this.GenerateRange(rd,range,j,i);
                    }
                    rd[i][0] = j;
                    rd[i][1] = j;
                }
            }
            /* if (len == 0){
                this.GenerateRange(rd,range,j);
            } */
            lastcell = data[j];
        }
        this.GenerateRange(rd,range,0,-1);
        return range;
    }

    SetStyle(dt){
        var datas = [];
        const style = {
            alignment: {
                horizontal: 'center',
                vertical: 'center',
            }
        }

        const titlestyle = {
            alignment: {
                horizontal: 'center',
                vertical: 'center',
            },
            font: {
                bold: true
            }
        }

        for (let i=0; i< dt.length; i++){
            var st = style;
            var temp = [];
            if (i == 0){
                st = titlestyle;
            }
            for (let j=0; j< dt[i].length; j++){
                if (!dt[i][j]){
                    dt[i][j] = '';
                }
                var cell = {v: dt[i][j], s: st};
                temp.push(cell); 
            }
            datas.push(temp);
        }

        return datas;
    }

    WriteFile(projectname,dt){
        var range = this.MergeData(dt);
        const option = {'!merges': range};
        var datas = this.SetStyle(dt);
        var buffer = xlsx.build([
            {
                name:'WorkSheet',
                data:datas
            }        
        ],option);
     
        //将文件内容插入新的文件中
        var filename = os.homedir() + '/' + projectname + '.xlsx';
        fs.writeFileSync(filename,buffer,{'flag':'w'});
    }
}

module.exports = XlsxHandler;